
package com.android.matmanual;
import java.io.IOException;


import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.hardware.Camera;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class FrontCameraTest extends Activity implements OnClickListener{
	SurfaceView surfaceView;
	Camera camera;
	boolean isPreview;
	Camera.Parameters parameters;
	WakeLock mWakeLock;
	
	private Button cameraSuccess, cameraFailure;
	private EngSqlite mEngSqlite;
	private String testCaseName;
  private int groupId;
  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setTitle(getResources().getString(R.string.checkcameratitle));
		   requestWindowFeature(Window.FEATURE_NO_TITLE);
			
		   //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
	    	        WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.camera_test);
		 PowerManager pm =
		           (PowerManager) getSystemService(POWER_SERVICE); 
		    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
		          "XYTEST"); 
		    mWakeLock.acquire(); 
		    
        groupId = this.getIntent().getIntExtra("groupId", 0);
        testCaseName = this.getIntent().getStringExtra("testname");
        cameraSuccess = (Button) findViewById(R.id.btn_pass);
        cameraFailure = (Button) findViewById(R.id.btn_fail);
        mEngSqlite = EngSqlite.getInstance(this);
        cameraSuccess.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mEngSqlite.updateDB(groupId,testCaseName, Const.SUCCESS);
            	finish();
            }
        });
        cameraFailure.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mEngSqlite.updateDB(groupId,testCaseName, Const.FAIL);
            	finish();
            }
        });
		surfaceView = (SurfaceView) findViewById(R.id.camera_view);
		surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		surfaceView.getHolder().addCallback(new SurfaceCallback());
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(camera!=null){
		    if(isPreview){
		    camera.stopPreview();
		    camera.release();
		    camera = null;
		    isPreview = false;
		    }
		 }
	}
	private class SurfaceCallback implements SurfaceHolder.Callback{
		public void surfaceCreated(SurfaceHolder holder) {	
		
		try {
			camera = Camera.open(1);
			WindowManager wm = (WindowManager) getSystemService(FrontCameraTest.this.WINDOW_SERVICE);
			Display display  = wm.getDefaultDisplay();
			parameters = camera.getParameters();
			parameters.setPreviewSize(display.getWidth(), display.getHeight());
			parameters.setPreviewFrameRate(3);
			parameters.setPictureFormat(PixelFormat.JPEG);
			parameters.setJpegQuality(85);
			//parameters.setPictureSize(display.getWidth(), display.getHeight());
			parameters.setPictureSize(display.getHeight(), display.getWidth());
			camera.setParameters(parameters);
			camera.setPreviewDisplay(surfaceView.getHolder());
			camera.startPreview();
			isPreview = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.notcheckcamera), 1).show();
		}
		 
	}
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
		int height) {
		// TODO Auto-generated method stub
		}         
		public void surfaceDestroyed(SurfaceHolder holder) {
			if(camera!=null){
			    if(isPreview){
			    camera.stopPreview();
			    camera.release();
			    camera = null;
			    isPreview = false;
			    }
			 }
	}
		   
 }
    @Override
    public void onConfigurationChanged(Configuration config) {
        super.onConfigurationChanged(config);
    }
    
    
	public void onClick(View view) {
		// TODO Auto-generated method stub
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		if (TestControl.IsAuto())
			setResult(TestControl.FALSE_RESULT);
        finish();
		super.onStop();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
}
		 